package encondingFilter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@WebFilter("/*")
public class PseudoSignInFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest hsrq = (HttpServletRequest) servletRequest;
        HttpServletResponse hsrep = (HttpServletResponse) servletResponse;

        String action = hsrq.getParameter("action");
        if (action != null &&action.equals("sign_in")){
            String refer = hsrq.getHeader("Referer");
            if (!refer.contains("index.jsp")){
                hsrq.setAttribute("error","Can't login");
                hsrq.getRequestDispatcher("/index.jsp").forward(hsrq,hsrep);
            }
        }
        filterChain.doFilter(hsrq,hsrep);
    }

}
